package p270jp.pxv.android.model;

import java.util.Date;
import java.util.List;
import p104de.greenrobot.dao.Property;
import p104de.greenrobot.dao.query.QueryBuilder;
import p104de.greenrobot.dao.query.WhereCondition;
import p210hh.PixivAccountManager;
import p270jp.pxv.android.legacy.constant.ContentType;
import p270jp.pxv.android.legacy.model.BrowsingHistory;
import p270jp.pxv.android.legacy.model.BrowsingHistoryDao;
import p270jp.pxv.android.legacy.model.DaoManager;
import p270jp.pxv.android.legacy.model.PixivWork;

/* renamed from: jp.pxv.android.model.BrowsingHistoryDaoManager */
/* loaded from: classes2.dex */
public class BrowsingHistoryDaoManager {
    public static final int DELETE_RECORDS_WHEN_OVER_MAX_RECORDS;
    public static final int MAX_RECORDS;
    private final DaoManager daoManager;
    private final PixivAccountManager pixivAccountManager;

    public BrowsingHistoryDaoManager(DaoManager daoManager, PixivAccountManager bVar) {
        this.daoManager = daoManager;
        this.pixivAccountManager = bVar;
    }

    public void deleteAll() {
        this.daoManager.getWritableSession().getBrowsingHistoryDao().deleteAll();
    }

    public void deleteByBrowsingHistories(List<BrowsingHistory> list) {
        this.daoManager.getWritableSession().getBrowsingHistoryDao().deleteInTx(list);
    }

    public List<BrowsingHistory> findIllusts(int i) {
        BrowsingHistoryDao browsingHistoryDao = this.daoManager.getReadableSession().getBrowsingHistoryDao();
        QueryBuilder<BrowsingHistory> queryBuilder = browsingHistoryDao.queryBuilder();
        QueryBuilder<BrowsingHistory> queryBuilder2 = browsingHistoryDao.queryBuilder();
        Property property = BrowsingHistoryDao.Properties.ContentType;
        queryBuilder.m7238j(queryBuilder2.m7241g(property.m7284a(ContentType.ILLUST.toString()), property.m7284a(ContentType.MANGA.toString()), new WhereCondition[0]), new WhereCondition[0]);
        queryBuilder.f12214g = Integer.valueOf(i);
        return queryBuilder.m7242f();
    }

    public List<BrowsingHistory> findNovels(int i) {
        QueryBuilder<BrowsingHistory> queryBuilder = this.daoManager.getReadableSession().getBrowsingHistoryDao().queryBuilder();
        queryBuilder.m7238j(BrowsingHistoryDao.Properties.ContentType.m7284a(ContentType.NOVEL.toString()), new WhereCondition[0]);
        queryBuilder.f12214g = Integer.valueOf(i);
        return queryBuilder.m7242f();
    }

    public void insertWithPixivWork(PixivWork pixivWork) {
        PixivAccountManager bVar = this.pixivAccountManager;
        if (bVar.f16506l && bVar.f16499e != pixivWork.user.f19078id) {
            ContentType a = ContentType.Companion.m4608a(pixivWork);
            BrowsingHistoryDao browsingHistoryDao = this.daoManager.getWritableSession().getBrowsingHistoryDao();
            QueryBuilder<BrowsingHistory> queryBuilder = browsingHistoryDao.queryBuilder();
            queryBuilder.m7238j(browsingHistoryDao.queryBuilder().m7247a(BrowsingHistoryDao.Properties.WorkId.m7284a(Long.valueOf(pixivWork.f19079id)), BrowsingHistoryDao.Properties.ContentType.m7284a(a.toString()), new WhereCondition[0]), new WhereCondition[0]);
            BrowsingHistory i = queryBuilder.m7239i();
            if (i != null) {
                i.setCreatedAt(new Date());
                browsingHistoryDao.update(i);
                return;
            }
            browsingHistoryDao.insert(new BrowsingHistory(null, Long.valueOf(pixivWork.f19079id), Long.valueOf(pixivWork.user.f19078id), a.toString(), new Date()));
            if (1000 < browsingHistoryDao.queryBuilder().m7244d()) {
                QueryBuilder<BrowsingHistory> queryBuilder2 = browsingHistoryDao.queryBuilder();
                queryBuilder2.m7240h(" ASC", BrowsingHistoryDao.Properties.CreatedAt);
                queryBuilder2.m7243e(100);
                browsingHistoryDao.deleteInTx(queryBuilder2.m7242f());
            }
        }
    }
}
